﻿@{
    ViewBag.Title = "Index";
}

<div class="row">
    <div class="col-xs-12">
        <p>
            <div class="row">
                <div class="col-sm-6">
                    <div class="input-group-btn">
                        <button class="btn btn-sm btn-primary" id="btnNew">
                            <i class="icon-plus bigger-110"></i>新增用户
                        </button>
                    </div>
                </div>
                <div class="col-sm-6">
                </div>
            </div>
        </p>
        @*<div class="table-header">
                用户列表
            </div>*@
        <div class="table-responsive">
            <table id="sample-table-2" class="table table-striped table-bordered table-hover"></table>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"
                        data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    创建新用户
                </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 账号 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 姓名 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 密码 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 密码确认 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 邮箱 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 联系电话 </label>
                        <div class="col-sm-9">
                            <input type="text" id="form-field-1" placeholder="Username" class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-sm btn-default"
                        data-dismiss="modal">
                    <i class="icon-remove"></i>
                    关闭
                </button>
                <button type="button" class="btn btn-sm btn-primary" onclick="onCreateUser()">
                    <i class="icon-ok"></i>
                    提交
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
@section inlineScript
{

    <script src='@Url.Content("~/Content/ace/js//jquery.dataTables.min.js")'></script>
    <script src='@Url.Content("~/Content/ace/js/jquery.dataTables.bootstrap.js")'></script>

    <script type="text/javascript">
        var oTable = null;
        $(document).ready(function () {
            $('#com-search-input').bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    loadUserList();
                }
            });
            $('#com-search-icon').on('click', function () {
                loadUserList();
            });
            $('#btnNew').on('click', function () {
                $('#myModal').modal();//{ backdrop: 'static', keyboard: false });
            });
            loadUserList();
        });

        function loadUserList() {
            if (oTable) {
                oTable.fnClearTable();
            }
            oTable = $("#sample-table-2").dataTable({
                bRetrieve :true,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 20,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "http://localhost:30837/api/bases/user/list",//这个是请求的地址
                "fnServerData": retrieveData, // 获取数据的处理函数
                "aoColumns": [
                    {
                        "sTitle": "Account",
                        mData: "account",
                        bSortable: true
                    },
                    {
                        "sTitle": "Name",
                        mData: "name",
                        bSortable: true
                    },
                    {
                        "sTitle": "Status",
                        "sClass": "center",
                        bSortable: false,
                        mData: "isEnabled",
                        "mRender": function (data, type, full) {
                            return data ? 'yes' : 'no';
                        }
                        //"fnRender": function (obj) {
                        //    var sReturn = obj.aData[obj.mDataProp];
                        //    if (sReturn == true) {
                        //        return "<b>已启用</b>";
                        //    }
                        //    return 'no';
                        //}
                    },
                    {
                        "sTitle": "Operation", mData: null,
                        "sWidth": '120px',
                        bSortable:false,
                        "mRender": function (data, type, full) {
                            return '<a href="#">删除</a>';
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        }

        // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
        function retrieveData(sSource, aoData, fnCallback) {
            var start = 0;
            var limit = 0;
            var propertyNames = new Array();
            var sortIndex = 0;
            var sortDirection;
            for (var index = 0; index < aoData.length; index++) {
                if (aoData[index].name == 'iDisplayStart') {
                    start = aoData[index].value;
                } else if (aoData[index].name == 'iDisplayLength') {
                    limit = aoData[index].value;
                } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                    propertyNames.push(aoData[index].value);
                } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                    sortIndex = aoData[index].value;
                } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                    sortDirection = aoData[index].value;
                }
            }
            $.ajax({
                url: sSource,//这个就是请求地址对应sAjaxSource
                data: { "filter": $('#com-search-input').val(), 'start': start, 'limit': limit, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        //把返回的数据传给这个方法就可以了,datatable会自动绑定数据的
                        //var rows = new Array();
                        //for (var index = 0; index < result.data.items.length; index++) {
                        //    rows.push([
                        //        result.data.items[index].account,
                        //        result.data.items[index].name,
                        //        result.data.items[index].isEnabled,
                        //        "<a href='#'>op</a>"
                        //    ]);
                        //}
                        fnCallback({
                            sEcho: aoData[0].value,
                            iTotalRecords: result.data.total,
                            iTotalDisplayRecords: result.data.total,
                            aaData: result.data.items
                        });
                    }
                },
                error: function (msg) {
                }
            });
        }

        function onCreateUser() {
            $('#myModal').modal('hide');
        }
    </script>
}
